package com.timu.kafka.springkafka;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.annotation.TopicPartition;

public class KafkaConsumerListener {
 
    @KafkaListener(topics = "cities")
    public void listen01(ConsumerRecord<String,String> consumerRecord){
        System.out.println("开始消费testTopic01的消息");
        System.out.println("消费者线程："+Thread.currentThread().getName()+"[ 消息 来自kafkatopic："+consumerRecord.topic()+",分区："+consumerRecord.partition() +" ，委托时间："+consumerRecord.timestamp()+"]消息内容如下：");
        System.out.println(consumerRecord.value());
    }
 
    @KafkaListener(topics = "cities")
    public void listen02(ConsumerRecord<String,String> consumerRecord){
        System.out.println("开始消费testTopic02的消息");
        System.out.println(consumerRecord.value());
    }
 
    /**
     * 消费 某个topic 下指定分区的消息
     */
    @KafkaListener(topicPartitions = {@TopicPartition(topic = "liuzebiao",partitions = {"1"})})
    public void topicMessage(ConsumerRecord<?, ?> record,String content){
        System.out.println("消息:"+ content);
        System.out.println("消息被消费------>Topic:"+ record.topic() + ",------>Value:" + record.value() +",------>Partition:" + record.partition());
    }
}